// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
// 引入element-ui,并注册
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import qs from 'qs'
import App from './App'
import router from './router'
import Vuex from 'vuex'
Vue.use(Vuex)
import "@/common/css/common.css"
import "@/common/css/font-awesome.css"
import {gets,sets,remove,clear} from './common/js/sessionStorage.js';//存入本地
import {get,post} from './common/js/http.js';//异步请求
import {combinationFormObject,formatData,timeDifference,convertFileName,getFileName,splitFileName,filterSrcContentLabel,fileGlobalFilter,getFileSuffix,renderSize,getParamValue} from './common/js/generalVue.js';//基础vuejs

/*接口地址*/
import urls from "./common/js/api_url_array.js";
import store from './common/js/store.js';

Vue.config.productionTip = false
/*本地存储*/
Vue.prototype.get = gets;
Vue.prototype.set = sets;
Vue.prototype.remove = remove;
Vue.prototype.clear = clear;
Vue.prototype.$axios = axios;

/*异步请求*/
Vue.prototype.$get = get;
Vue.prototype.$post = post;

/*接口地址*/
Vue.prototype.urls=urls;
//Vue.prototype.common=common;

/*基础方法*/
Vue.prototype.combinationFormObject=combinationFormObject;
Vue.prototype.formatData=formatData;
Vue.prototype.timeDifference=timeDifference;
Vue.prototype.convertFileName=convertFileName;
Vue.prototype.getFileName=getFileName;
Vue.prototype.splitFileName=splitFileName;
Vue.prototype.filterSrcContentLabel=filterSrcContentLabel;
Vue.prototype.fileGlobalFilter=fileGlobalFilter;
Vue.prototype.getFileSuffix=getFileSuffix;
Vue.prototype.renderSize=renderSize;
//获取地址栏参数
Vue.prototype.getParamValue=getParamValue;
// 添加请求拦截器，在请求头中加token
axios.interceptors.request.use(
  config => {
    let token = gets('token');
    //let token = sessionStorage.getItem('token');
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  });

// http response 拦截器
axios.interceptors.response.use(
  response => {
    // let token = sessionStorage.getItem('Authorization');
    // if(token){//有没有token
    //   }else window.isReresh = false;
    // }
    return response;  //请求成功的时候返回的data
  },
  error => {
    try {
      if (error.response) {
        switch (error.response.status) {
          case 401://token过期，清除它,清除token信息并跳转到登录页面
            //清除token
            sets("token", "");//token
            //跳转到登录页面
            router.replace({
              path: '/login'
            });
            return;
          case 403://权限
            //清除token
            sets("token", "");//token
            //跳转到登录页面
            router.replace({
              path: '/login'
            });
            return;
        }
      }
      return Promise.reject(error.response.data)
    }
    catch (e) {
    }
  });

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
